﻿using Myotragus.Data.Containers;
using Myotragus.Store.Model.Tests.Services;
using NUnit.Framework;

namespace Myotragus.Store.Model.Tests
{
  [TestFixture]
  public class ServicesContainerFixture
  {
    IServicesContainer Container
    {
      get;
      set;
    }

    [SetUp]
    public void SetUp()
    {
      var repo = ContainerHelper.CreateContainer();

      repo.Register(typeof(ServiceA), typeof(ServiceB), typeof(ServiceAb));

      Container = repo;
    }

    [Test]
    public void FailedTestEveryResolveMethod()
    {
      Assert.Throws(typeof(Castle.MicroKernel.ComponentNotFoundException),
        () => Container.Resolve<IServiceC>());
      Assert.Throws(typeof(Castle.MicroKernel.ComponentNotFoundException),
        () => Container.Resolve(typeof(IServiceC)));
    }

    [Test]
    public void SuccessTestEveryResolveMethod()
    {
      var res = Container.Resolve<IServiceA>();
      Assert.AreEqual(res, Container.Resolve(typeof(IServiceA)));
    }

    [Test]
    public void TestForCorrentSelectionOnOverlappingInterfaces()
    {
      var sa = Container.Resolve<IServiceA>();
      var sb = Container.Resolve<IServiceB>();
      var sab = Container.Resolve<IServiceAB>();

      Assert.AreNotEqual(sa, sab);
      Assert.AreNotEqual(sb, sab);
      Assert.AreNotEqual(sa, sb);

      Assert.AreEqual(sa.Services, Container);
      Assert.AreEqual(sb.Services, Container);
      Assert.AreEqual(sab.Services, Container);

      Assert.AreEqual(sa, Container.Resolve(typeof(IServiceA)));
      Assert.AreEqual(sb, Container.Resolve(typeof(IServiceB)));
      Assert.AreEqual(sab, Container.Resolve(typeof(IServiceAB)));

      sa.Foo();
      sab.Foo();

      sb.Goo();
      sab.Goo();
    }
  }
}
